<extend name='Index/index' />

<block name="main">
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">用户管理　
        <if condition="in_array('User/add',$access)">
        <button class="btn btn-default disabled" data-toggle="modal" data-target="#myModaladd">添加用户</button></h1>
        <else/>
        <button class="btn btn-default" data-toggle="modal" data-target="#myModaladd">添加用户</button></h1>
        </if>

    </div>
    <div class="wrapper-md">
        <div class="panel panel-default">
            <div class="table-responsive">
                <table class="table table-striped b-t b-light">
                    <thead>
                        <tr>
                            <th  width="4%"><div align='center'>ID</div></th>
                            <th  width="10%"><div align='center'>用户名</div></th>
                            <th  width="8%"><div align='center'>昵称</div></th>
                            <th  width="5%"><div align='center'>性别</div></th>
                            <th  width="10%"><div align='center'>手机号</div></th>
                            <th  width="10%"><div align='center'>生日</div></th>
                            <th  width="5%"><div align='center'>状态</div></th>
                            <th  width="10%"><div align='center'>操作</div></th>
                        </tr>
                    </thead>
                    <tbody>
                        <foreach name="data" item="vo" key='k'>
                            <tr>
                                <td align='center'><{$vo.ui_id}></td>
                                <td align='center'><{$vo.email}></td>
                                <td align='center'><{$vo.nickname}></td>
                                <td align='center'><?php echo $vo['sex']==0? 男: 女?></td>
                                <td align='center'><{$vo.phone}></td>
                                <td align='center'><{$vo.birthday}></td>
                                <td align='center'><a href=""><?php echo $vo['status']==0? 禁用: 激活?></a>
                                </td>
                                <td align='center'>
                                
                                <if condition="in_array('User/edit',$access)">
                                <a href="#"  class="disabled btn" data-toggle="modal" data-target="#myModal">编辑</a>
                                <else/>
                                <a href="#"  data-toggle="modal" data-target="#myModal" class="btn btn-default">编辑</a>
                                </if>
                                <if condition="in_array('User/del',$access)">
                                  <a href="" class="disabled btn">删除</a>
                                <else/>
                                  <a href=""  class="btn btn-default">删除</a>
                                </if>
                                    
                                </td>
                            </tr>
                        </foreach>
                    </tbody>
                </table>
            </div>
        </div>
        <div class="result page"><{$page}></div>
        <p></p>
        <p></p>
    </div>

    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                    </button>
                    <h4 class="modal-title" id="myModalLabel">编辑用户</h4>
                </div>
                <div class="modal-body">

                    <form class="form-horizontal" role="form" id="editform">
                        <div class="form-group">
                            <label for="inputNickname" class="col-sm-2 control-label"">昵称</label>
                            <div class="col-sm-9">
                                <input type="email" class="form-control" id="inputNickname">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputPhone" class="col-sm-2 control-label">手机号</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="inputPhone" maxlength=11>
                            </div>
                        </div>

                        <div class="form-group">
                            <span class="col-sm-2 control-label">性别</span>
                            <div class="col-sm-offset-0 col-sm-8">
                                <div class="radio" id="radioSex">
                                    <label>
                                        <input type="radio" name="sex" value=0>男
                                    </label>
                                    <label>
                                        <input type="radio" name="sex" value=1>女
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <span class="col-sm-2 control-label">出生日期</span>
                            <div class="col-sm-offset-0 col-sm-8">
                                <div class="radio">
                                    <input type="date" name="birthday" id="dateBirthday">
                                </div>
                            </div>
                        </div>
                        <input type="hidden" id="getemail">
                    </form>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal" id="modalclose">Close</button>
                    <button type="button" class="btn btn-primary" id="submit">Save changes</button>
                </div>
            </div>
        </div>
    </div>
<!-- ========================================================================================================== -->
    <!-- Modal -->
    <div class="modal fade" id="myModaladd" tabindex="-1" role="dialog" aria-labelledby="myModalLabel_add" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                    </button>
                    <h4 class="modal-title" id="myModalLabel_add">添加用户</h4>
                </div>
                <div class="modal-body">

                    <form class="form-horizontal" role="form" id=addform>
                        <div class="form-group">
                            <label for="addEmail" class="col-sm-2 control-label"">Email</label>
                            <div class="col-sm-9">
                                <input type="email" class="form-control" id="addEmail">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="addPassword" class="col-sm-2 control-label"">Password</label>
                            <div class="col-sm-9">
                                <input type="password" class="form-control" id="addPassword">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="addNickname" class="col-sm-2 control-label"">昵称</label>
                            <div class="col-sm-9">
                                <input type="email" class="form-control" id="addNickname">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="addPhone" class="col-sm-2 control-label">手机号</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="addPhone" maxlength=11>
                            </div>
                        </div>

                        <div class="form-group">
                            <span class="col-sm-2 control-label">性别</span>
                            <div class="col-sm-offset-0 col-sm-8">
                                <div class="radio" id="addSex">
                                    <label>
                                        <input type="radio" name="sex" value=0>男
                                    </label>
                                    <label>
                                        <input type="radio" name="sex" value=1>女
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <span class="col-sm-2 control-label">出生日期</span>
                            <div class="col-sm-offset-0 col-sm-8">
                                <div class="radio">
                                    <input type="date" name="birthday" id="addBirthday">
                                </div>
                            </div>
                        </div>
                    </form>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal" id="addmodalclose">Close</button>
                    <button type="button" class="btn btn-primary" id="addsub">Save changes</button>
                </div>
            </div>
        </div>
    </div>
    
    <script>
        $("a:contains('编辑')").click(function(){
            var nickname = $(this).parents('tr').children('td').eq(2).html();
            var phone = $(this).parents('tr').children('td').eq(4).html();
            var sex = $(this).parents('tr').children('td').eq(3).html();
            var birthday = $(this).parents('tr').children('td').eq(5).html();
            var email = $(this).parents('tr').children('td').eq(1).html();
            $('#inputNickname').val(nickname);
            $('#inputPhone').val(phone);
            $("#editform label:contains("+sex+")").children('input').attr('checked',true);
            $('#dateBirthday').val(birthday);
            $('#getemail').val(email);
        })

        $('#submit').click(function(){
            var nickname = $('#inputNickname').val();
            var phone = $('#inputPhone').val();
            var birthday = $('#dateBirthday').val();
            var sex = $('#editform :checked').val();
            var email = $('#getemail').val();

            $.ajax({
                type:'post',
                url:"<{:U('User/edit')}>",
                data:{'nickname':nickname,'phone':phone,'sex':sex,'birthday':birthday,'email':email},
                success:function(data){
                    alert(data);
                    $('#modalclose').click();
                    $('td:contains('+email+')').next('td').html(nickname);
                    $('td:contains('+email+')').parent().children().eq(4).html(phone);
                    $('td:contains('+email+')').parent().children().eq(5).html(birthday);
                    $('td:contains('+email+')').parent().children().eq(3).html($(':checked').parent().text())
                }
            })
        })

        $("a:contains('删除')").click(function(){
            if (confirm('确认删除?')) {
                var email = $(this).parent().siblings().eq(1).html();
                var tr = $(this).parents('tr');
                $.ajax({
                    url: "<{:U('User/del')}>",
                    data:{'email':email},
                    type: 'get',
                    success: function(data){
                        alert(data);
                        tr.remove();
                    }
                })
            }
        })


        $('#addsub').click(function(){
            var email = $('#addEmail').val();
            var pwd = $('#addPassword').val();
            var nickname = $('#addNickname').val();
            var phone = $('#addPhone').val();
            var birthday = $('#addBirthday').val();
            var sex = $('#addform :checked').val();
            if (email=='') {
                alert('邮箱不能为空');
                return false;
            }
            if (pwd=='') {
                alert('密码不能为空');
                return false;
            }
            var url = "<{:U('User/index')}>";
            $.ajax({
                type:'post',
                url:"<{:U('User/add')}>",
                data:{'nickname':nickname,'password':pwd,'phone':phone,'sex':sex,'birthday':birthday,'email':email},
                success:function(data){
                    alert(data);
                    $('#addmodalclose').click();
                    window.location.href = url;
                }
            })
        })

    </script>


</block>